package com.huan.utils;

import com.fasterxml.jackson.databind.util.BeanUtil;
import com.huan.VO.HotArticleVo;
import com.huan.domain.entity.Article;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.stream.Collectors;

public class BeanCopyUtil {
    public BeanCopyUtil() {
    }
    public static <T> T resultVO(Object source,Class<T> tClass){
        T result = null;
        try {
            result = tClass.getDeclaredConstructor(null).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        BeanUtils.copyProperties(source,result);
        return result;
    }
    public static <O,T> List<T> resultAllVo(List<O> source,Class<T> tClass){
        return source.stream()
                .map(result->resultVO(result,tClass))
                .collect(Collectors.toList());

    }

    public static void main(String[] args) {
        Article article = new Article();
        article.setId(2L);
        article.setViewCount(10L);
        HotArticleVo hotArticleVo = resultVO(article, HotArticleVo.class);
        System.out.println(hotArticleVo);
    }
}
